﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using FileParser;

namespace SimpleXMLParser
{
    /// <summary>
    /// This class parses xml files that are output by Doxygen in order to get 
    /// the relevant information such as class name, inheritance, attributes and methods
    /// of each file for each revision of the project being tested and then passes it
    /// to the graphics module to be processed.
    /// </summary>
	class C_XmlParser
    {
        XmlTextReader textReader;
        XmlDocument doc;
        XmlElement root;
        XmlNodeList nodes;
        ArrayList classes;
        XmlNodeList innerNodes;
        String filePath;

        XmlNodeList inheritanceNodes;

        public C_XmlParser(string filePath1) 
        {
            this.filePath = filePath1;

            try
            {
                ///Creation of an xml text reader to traverse xml files
                textReader = new XmlTextReader(filePath + "index.xml");
                textReader.Read();

                doc = new XmlDocument();

                doc.Load(textReader);

                root = doc.DocumentElement;
                ///Accessing specific xml nodes to get the relevant information from the files
                nodes = root.SelectNodes("/doxygenindex/compound");
                innerNodes = root.SelectNodes("/doxygenindex/compound/member");
                classes = new ArrayList();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                textReader.Close();///close the opened file
            }
        }

        ///Method to generate the data structure, which needs to be send to graphics module
		public ArrayList GenerateData()
		{
                String name;
                C_DataStructure tmp;

                try
                {
                    foreach (XmlNode node in nodes)
                    {
                        if (node.Attributes["kind"].Value == "class")
                        {
                            name = node["name"].InnerText;
                            tmp = new C_DataStructure(name);

                            tmp.SetRefId(node.Attributes["refid"].Value);

                            foreach (XmlNode innerNode in innerNodes)
                            {

                                if (innerNode.ParentNode["name"].InnerText == name)
                                {

                                    if (innerNode.Attributes["kind"].Value == "variable")
                                    {
                                        tmp.AddVariable(node["name"].InnerText);
                                        
                                    }
                                    else if (innerNode.Attributes["kind"].Value == "function")
                                    {
                                        tmp.AddFunction(node["name"].InnerText);
                                        
                                    }
                                }

                            }

                            classes.Add(tmp);
                            

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                GetInheritance();

                return classes;
		}
	
        ///This method goes to the appropriated file and gets the inheritance of the class being evaluated
        public void GetInheritance(){

            foreach (C_DataStructure n in classes)
            {
               
                String tmpRefId = n.GetRefId();

                try
                {

                    ///This line generates the path to look for the right file and find inheritance
                    XmlTextReader tmpTextReader = new XmlTextReader(filePath + tmpRefId + ".xml");
                    tmpTextReader.Read();

                    XmlDocument tmpDoc = new XmlDocument();
                    tmpDoc.Load(tmpTextReader);

                    XmlElement currentFile = tmpDoc.DocumentElement;
                    inheritanceNodes = currentFile.SelectNodes("/doxygen/compounddef");

                    String inheritingClass = null;
                    bool inheritance = false;

                    tmpTextReader.Close();///close the open xml file


                    foreach (XmlNode nn in inheritanceNodes)
                    {

                        XmlNodeList compounddefNodes = nn.ChildNodes;

                        foreach(XmlNode n1 in compounddefNodes)
                        {
                            if (n1.Name == "basecompoundref")
                            {
                                Console.WriteLine(n1.Attributes["refid"].Value + " Class name: " + n.GetName());
                                inheritingClass = n1.Attributes["refid"].Value;
                                inheritance = true;
                                

                            }
                        }
                        
                    }

                    if (!inheritance)
                        inheritingClass = "none";

                    n.SetInheritance(inheritingClass);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                
            }

        }
    
    }
}
